﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using erpIngesup.Functions;
using erpIngesup.Areas.Eleve.Models;

namespace erpIngesup.Areas.Eleve.Controllers
{
    public class ModulesController : Controller
    {
        private bddIngesupEntitiesEleve db = new bddIngesupEntitiesEleve();
        //
        // GET: /Eleve/Modules/

        public ActionResult Index(string langue)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "stevix";
            string year = DateTime.Now.Year.ToString();
            int classe = db.ELEVECLASSE.SingleOrDefault(x => x.LOGINPERS == login && x.ANNEECLASSE == year).IDCLASSE;
            var module = db.MODULE.Where(x => x.CLASSE.IDCLASSE == classe).ToList();
            return View(module);
        }

        public ActionResult Details(string langue, string module)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            var mods = db.MODULE.Single(x => x.LIB_MOD == module);
            return View(mods);
        }

        public FilePathResult Open(string langue, string nomDoc)
        {
            FilePathResult file = File(nomDoc, "");
            return file;
        }

        public ActionResult DetailsProject(string langue, int idProject)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            var project = db.PROJET.Single(x => x.IDPROJET == idProject);
            return View(project);
        }

        public ActionResult DetailsGroupe(string langue, GROUPE gr)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            return View(gr);
        }

        public ActionResult Inscription(string langue, int idGroupe)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            var gr = db.GROUPE.Single(x => x.IDGROUPE == idGroupe);
            return View(gr);
        }

        [HttpPost]
        public ActionResult Inscription(string langue, GROUPE groupeToSave)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            ELEVE e = db.ELEVE.Single(x => x.LOGINPERS == login);
            if (!groupeToSave.ELEVE.Contains(e))
            {
                groupeToSave.ELEVE.Add(e);
                db.SaveChanges();
                return RedirectToAction("DetailsGroupe", new { groupe = groupeToSave });
            }
            return View();
        }

        public ActionResult AddGroup(string langue, int idProjet)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
            string login = Session["login"] != null ? Session["login"].ToString() : "";
            var projet = db.PROJET.Single(x => x.IDPROJET == idProjet);
            return View(projet);
        }

        [HttpPost]
        public ActionResult AddGroup(string langue, PROJET pr)
        {
            try
            {
                System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Field.getLanguage(langue));
                string login = Session["login"] != null ? Session["login"].ToString() : "";
                db.Refresh(System.Data.Objects.RefreshMode.StoreWins, pr);
                db.SaveChanges();
                return RedirectToAction("DetailsProject", new { idProject = pr.IDPROJET });
            }
            catch (Exception e)
            {
                return View();
            }
        }

        public PartialViewResult Groupe(string langue, PROJET projet)
        {
            GROUPE gr = new GROUPE();
            projet.GROUPE.Add(gr);
            return PartialView(projet);
        }
    }
}
